#include "led.h"

/**
 * @name led初始化
 * @description
 * @function
 * @returns {void}
 * @requires []
 */
void led_init(void) {
    uint32_t bit = 1;
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    // 使能时钟
    LED_RCC_AHBPeriphClockCmd;
    // 配置GPIO
    GPIO_InitStruct.GPIO_Pin = LED_GPIO_Pin;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    // 初始GPIO
    GPIO_Init(LED_GPIO, &GPIO_InitStruct);
    // 默认熄灭
    GPIO_SetBits(LED_GPIO, LED_GPIO_Pin);
}

/**
 * @name led控制
 * @description
 * @function
 * @param {LED} led LED
 * @param {LED_STATE} state LED状态
 * @returns {void}
 * @requires [led,state]
 */
void led_control(const LED led, const LED_STATE state) {
    // 设置状态
    if (state)
        GPIO_ResetBits(LED_GPIO, led);
    else
        GPIO_SetBits(LED_GPIO, led);
}

/**
 * @name led状态
 * @description
 * @function
 * @param {LED} led LED
 * @returns {LED_STATE} LED状态
 * @requires [led]
 */
LED_STATE led_state(const LED led) {
    // 读取状态
    return GPIO_ReadInputData(LED_GPIO) & led ? LED_OFF : LED_ON;
}
